/*
 * This file is part of nettool4j.
 * 
 * rockframework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * rockframework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>;.
 */
package net.woodstock.nettool4j.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Level;

import net.woodstock.nettool4j.AbstractRunnable;

public class TCPRedirectHandler extends AbstractRunnable {

	private Socket	socketInput;

	private Socket	socketOutput;

	private long	count;

	public TCPRedirectHandler(final Socket socketInput, final Socket socketOutput) {
		super();
		this.socketInput = socketInput;
		this.socketOutput = socketOutput;
		this.count = 0;
	}

	@Override
	public void run() {
		try {
			InputStream inputStream = this.socketInput.getInputStream();
			OutputStream outputStream = this.socketOutput.getOutputStream();

			int i = -1;
			do {
				if (!this.socketInput.isInputShutdown()) {
					i = inputStream.read();
					if (i != -1) {
						this.count++;
						if (!this.socketOutput.isOutputShutdown()) {
							outputStream.write(i);
						} else {
							break;
						}
					}
				} else {
					break;
				}
			} while (i != -1);
		} catch (IOException e) {
			this.getLogger().log(Level.FINEST, e.getMessage(), e);
		} finally {
			String addressInput = SocketUtils.toString(this.socketInput.getLocalSocketAddress());
			String addressOutput = SocketUtils.toString(this.socketInput.getRemoteSocketAddress());
			this.getLogger().log(Level.INFO, "Disconecting " + addressInput + ":" + " => " + addressOutput + " [" + this.count + " bytes]");
			try {
				this.socketInput.shutdownInput();
				this.socketInput.shutdownOutput();
				this.socketInput.close();
				// this.socketOutput.shutdownInput();
				// this.socketOutput.shutdownOutput();
			} catch (IOException e) {
				this.getLogger().log(Level.FINEST, e.getMessage(), e);
			}
		}
	}

}
